package map.android.baidu.carpool.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.baidu.components.api.extra.APIProxy;
import com.baidu.mapframework.widget.BMAlertDialog;
import map.android.baidu.carpool.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class SceneryUploadPicUtil {
    
    public static BMAlertDialog uploadListDialog;

    //压缩图片的方法
    public static void compressBmpToFile(Bitmap bmp,File paramfile){ 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        int options = 80;//个人喜欢从80开始,  
        bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
        while (baos.toByteArray().length / 1024 > 100) {   
            baos.reset();  
            options -= 10;  
            bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
        }  
        try {  
            FileOutputStream fos = new FileOutputStream(paramfile);  
            fos.write(baos.toByteArray());  
            fos.flush();  
            fos.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    
    
    
    //详情页和发布页共用dialog
    public void showCommentDialog(android.widget.AdapterView.OnItemClickListener uploadAlertDialogListener , Context mContext) {
        ArrayList<String> listItem = new ArrayList<String>();
        listItem.add("拍照");
        listItem.add("从相册上传");
        listItem.add("取消");
        ListView listView = new ListView(mContext);
        UploadPicAdapter<String> adapter = new UploadPicAdapter<String>(mContext, listItem);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(uploadAlertDialogListener);
        uploadListDialog = new BMAlertDialog.Builder(
                com.baidu.mapframework.app.fpstack.TaskManagerFactory.getTaskManager().getContext()).setView(listView).create();
        uploadListDialog.show();
    }

    public class UploadPicAdapter<T> extends ArrayAdapter<T> {
        
        public UploadPicAdapter(Context context, List<T> list) {
            super(context, -1, -1, list);
        }
        
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if (getContext() == null) {
                return null;
            }

            RelativeLayout itemView = (RelativeLayout) APIProxy.res().getInflater().inflate(R.layout.alertdialog_image_text, null);
            TextView itemTextView = (TextView)itemView.findViewById(R.id.text_content);
            String text = (String)getItem(position);
            itemTextView.setText(text);

            if(text.equals("取消")) {
                itemView.setBackgroundResource(R.drawable.alertdialog_list_item_dark_selector);
            }  else{
                itemView.setBackgroundResource(R.drawable.alertdialog_list_item_bg_selector);
            }

            return itemView;
        }
    }
    
}
